package com.sls.web.manager.game.client;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.game.constants.ResultCode;
import com.sls.web.manager.game.pojo.ekhui.Account;
import com.sls.web.manager.game.pojo.ekhui.CommissionPay;
import com.sls.web.manager.pojo.UserInfoVO;

public class CommissionPayClient {
	private static Logger log = LoggerFactory.getLogger(CommissionPayClient.class) ;
	private final static Lock lock = new ReentrantLock() ;
	private volatile static CommissionPayClient instance = null ;
	private CommissionPayClient(){
		super() ;
	}
	public static CommissionPayClient getInstance(){
		if(instance == null){
			try {
				lock.lock();
				if (instance == null) {
					instance = new CommissionPayClient();
				}
			} finally{
				lock.unlock() ;
			}
		}
		return  instance ;
	}
	public boolean saveCommissionPay(Account payAccount ,Account commissionAccount , CommissionPay commissionPay ,UserInfoVO userInfoVO ){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setServiceCode("300041") ;
		head.setUserId(userInfoVO.getUserId());
		head.setRequestId(UUID.randomUUID().toString()) ;
		commissionPay.setOperatorId(userInfoVO.getUserId()) ;
		commissionPay.setOperatorName(userInfoVO.getUserName()) ;
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("payAccount", payAccount) ;
		paramMap.put("commissionAccount", commissionAccount) ;
		paramMap.put("commissionPay", commissionPay) ;
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			if(StringUtils.isNotBlank(body)){
				Gson gson = new Gson() ;
				return gson.fromJson(body, new TypeToken<Boolean>(){}.getType()) ;
			}
		}
		return false ;
	}
}
